import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
  {
    path: '/:pathMatch(.*)',
    name: 'error',
    component: () => import( '../views/ErrorView.vue'),
  },
  {
    path: '/',
    name: 'index',
    component: () => import( '../views/Login.vue'),
  },
  {
    path: '/login',
    name: 'login',
    component: () => import( '../views/Login.vue')
  },
  {
    path: '/workerlogin',
    name: 'workerlogin',
    component: () => import( '../views/WorkerLogin.vue')
  },
  {
    path: '/adminlogin',
    name: 'adminlogin',
    component: () => import( '../views/AdminLogin.vue')
  },
  {
    path: '/uregister',
    name: 'urgister',
    component: () => import( '../views/Uregister.vue')
  },
  {
    path: '/wregister',
    name: 'wrgister',
    component: () => import( '../views/Wregister.vue')
  },
  {
    path: '/lssueorder',
    name: 'lssueorder',
    component: () => import( '../views/LssueOrder.vue'),
    children:[
      {
        path:'buy1',
        name:'buy1',
        component: () => import( '../views/Buy.vue')
      },
      {
        path:'send',
        name:'send',
        component: () => import( '../views/Send.vue')
      },
      {
        path:'sub',
        name:'sub',
        component: () => import( '../views/Substitution.vue')
      },
      {
        path:'done',
        name:'done',
        component: () => import( '../views/Done.vue')
      },
      {
        path:'undone',
        name:'undone',
        component: () => import( '../views/Undone.vue')
      },
      {
        path:'profile',
        name:'profile',
        component: () => import( '../views/UInformation.vue')
      },
      {
        path: '/umessage',
        name: 'umessage',
        component: () => import( '../views/UMessage.vue')
      },
    ],
  },
    {
      path: '/aadministrators',
      name: 'administrators',
      meta:{requireAuth:true}/* 登录后才可以使用该组件 */,
      component: () => import( '../views/AAdministrators.vue'),
      children:[
      {
        path: '/worker',
        name: 'worker',
        component: () => import( '../views/AWorkerView.vue')
      },
      {
        path: '/user',
        name: 'user',
        component: () => import( '../views/AUserView.vue')
      },
      {
        path: '/order',
        name: 'order',
        component: () => import( '../views/AOrderView.vue')
      },
      {
        path: '/ad',
        name: 'ad',
        component: () => import( '../views/AAdView.vue')
      },
      {
        path: '/message',
        name: 'message',
        component: () => import( '../views/AMessageView.vue')
      },
      {
        path: '/cashout',
        name: 'cashout',
        component: () => import( '../views/ACashout.vue')
      },
      {
        path: '/cashoutok',
        name: 'cashoutok',
        component: () => import( '../views/ACashoutok.vue')
      },
    ]
  },
  {
    path: '/workers',
    name: 'workers',
    component: () => import( '../views/Workers.vue'),
    children:[
      {
        path: '/take',
        name: 'take',
        component: () => import( '../views/WTakeView.vue')
      },
      {
        path: '/give',
        name: 'give',
        component: () => import( '../views/WGiveView.vue')
      },
      {
        path: '/buy',
        name: 'buy',
        component: () => import( '../views/WBuyView.vue')
      },
      {
        path: '/ing',
        name: 'ing',
        component: () => import( '../views/WingView.vue')
      },
      {
        path: '/ok',
        name: 'ok',
        component: () => import( '../views/WokView.vue')
      },
      {
        path: '/okhas',
        name: 'okhas',
        component: () => import( '../views/Wokhas.vue')
      },
      {
        path: '/information',
        name: 'information',
        component: () => import( '../views/WInformation.vue')
      },
      {
        path: '/information',
        name: 'information',
        component: () => import( '../views/WInformation.vue')
      },
      {
        path: '/wmessage',
        name: 'wmessage',
        component: () => import( '../views/WMessage.vue')
      },
    ]
  },
  {
    path: '/usersfirst',
    name: 'usersfirst',
    component: () => import( '../views/UsersFirst.vue')
  },
  {
    path: '/workersfirst',
    name: 'workersfirst',
    component: () => import( '../views/WorkersFirst.vue')
  },
]
  

    




// 页面刷新时，重新赋值token

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to,from,next)=>{
  var token=localStorage.getItem("token");/* 等于 var token=localStorage.token;*/
  if(to.meta.requireAuth){
    if(token){
      next();
    }
    else{/* beforeEach有if必须有esle */
      next({name:"login"});
      alert("您需要登录")
    }
  }
  else{/* beforeEach有if必须有esle */
    next();
  }
});

export default router
